package com.eipc.util;


import com.eipc.model.Consult;
import com.eipc.model.Game;
import com.eipc.model.ImportNotice;
import com.eipc.model.ImportNoticeChild;
import com.eipc.model.Match;
import com.eipc.model.Player;
import com.eipc.model.PlayerGroup;
import com.eipc.model.Reply;
import com.eipc.model.Score;
import com.eipc.model.Venue;
import com.eipc.model.Weather;

public class DataParse {
	public static final String PREX = ";;";
	// serverid,appType,{content},{imageWebUrl},issuer,issueTime,sender,sendTime,sendType,top	
	public static ImportNotice ParseImportNotice(String strImportNotice){
		try{
			String[] myString = strImportNotice.split(PREX);	
			if (myString.length == 13){
				ImportNotice myImportNotice = new ImportNotice(
						Integer.valueOf(myString[0]),
						Integer.valueOf(myString[1]),
						myString[2].substring(1, myString[2].length()-1),
						myString[3].substring(1, myString[3].length()-1),
						"",
						myString[4],
						myString[5],
						myString[6],
						myString[7],
						myString[10],
						myString[8],
						myString[11],
						myString[12],
						myString[9]);
				return myImportNotice;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	

	public static String formatImportNotice(ImportNotice in){
		if(in!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(in.id);
			sb.append(PREX+getValue(in.appType));
			sb.append(PREX+"{"+getValue(in.content+"}"));
			sb.append(PREX+"{"+getValue(in.imageWebUrl+"}"));
			sb.append(PREX+getValue(in.issuer));
			sb.append(PREX+getValue(in.issueTime));
			sb.append(PREX+getValue(in.sender));
			sb.append(PREX+getValue(in.sendTime));
			sb.append(PREX+getValue(in.sendType));
			sb.append(PREX+getValue(in.top));
			return sb.toString();
		}else{
			return null;
		}
	}
	
	
	public static String formatWeather(Weather we){
		if(we!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(we.id);
			sb.append(PREX+getValue(we.date));
			sb.append(PREX+getValue(we.message));
			sb.append(PREX+getValue(we.typePeriod1));
			sb.append(PREX+getValue(we.statusPeriod1));
			sb.append(PREX+getValue(we.typePeriod2));
			sb.append(PREX+getValue(we.statusPeriod2));
			sb.append(PREX+getValue(we.typePeriod3));
			sb.append(PREX+getValue(we.statusPeriod3));
			sb.append(PREX+getValue(we.typePeriod4));
			sb.append(PREX+getValue(we.statusPeriod4));
			sb.append(PREX+getValue(we.typePeriod5));
			sb.append(PREX+getValue(we.statusPeriod5));
			sb.append(PREX+getValue(we.typePeriod6));
			sb.append(PREX+getValue(we.statusPeriod6));
			sb.append(PREX+getValue(we.typePeriod7));
			sb.append(PREX+getValue(we.statusPeriod7));
			sb.append(PREX+getValue(we.typePeriod8));
			sb.append(PREX+getValue(we.statusPeriod8));
			sb.append(PREX+getValue(we.typePeriod9));
			sb.append(PREX+getValue(we.statusPeriod9));
			sb.append(PREX+getValue(we.typePeriod10));
			sb.append(PREX+getValue(we.statusPeriod10));
			sb.append(PREX+getValue(we.typePeriod11));
			sb.append(PREX+getValue(we.statusPeriod11));
			sb.append(PREX+getValue(we.typePeriod12));
			sb.append(PREX+getValue(we.statusPeriod12));
			sb.append(PREX+getValue(we.typeHalf1));
			sb.append(PREX+getValue(we.statusHalf1));
			sb.append(PREX+getValue(we.typeHalf2));
			sb.append(PREX+getValue(we.statusHalf2));
			sb.append(PREX+getValue(we.typeHalf3));
			sb.append(PREX+getValue(we.statusHalf3));
			sb.append(PREX+getValue(we.typeHalf4));
			sb.append(PREX+getValue(we.statusHalf4));
			sb.append(PREX+getValue(we.updateTime));
			sb.append(PREX+getValue(we.version));
			sb.append(PREX+getValue(we.temperature));
			
			return sb.toString();
		}else{
			return null;
		}
	}
	
	public static Weather ParseWeather(String strWeather){
		try{
			String[] myString = strWeather.split(PREX);	
			if (myString.length == 39){
				Weather myWeather = new Weather(Integer.valueOf(myString[0]),
						myString[1],
						myString[2],
						myString[3],
						myString[4],
						myString[5],
						myString[6],
						myString[7],
						myString[8],
						myString[9],
						myString[10],
						myString[11],
						myString[12],
						myString[13],
						myString[14],
						myString[15],
						myString[16],
						myString[17],
						myString[18],
						myString[19],
						myString[20],
						myString[21],
						myString[22],
						myString[23],
						myString[24],
						myString[25],
						myString[26],
						myString[27],
						myString[28],
						myString[29],
						myString[30],
						myString[31],
						myString[32],
						myString[33],
						myString[34],
						com.eipc.util.Tools.getCurrentTimeString(),
						myString[36],
						myString[38],
						myString[37]);
				return myWeather;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	// int id, String consultId, String content, String sendTime	
	public static Consult ParseConsult(String strConsult){
		try{
			String[] myString = strConsult.split(PREX);	
			if (myString.length == 3){
				Consult myConsult = new Consult(0,myString[0],myString[1],myString[2]);
				return myConsult;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	public static Reply ParseReply(String strConsult){
		try{
			String[] myString = strConsult.split(PREX);	
			if (myString.length == 3){
				Reply r = new Reply("",myString[0],myString[1],myString[2]);
				return r;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}

	public static String formatConsult(Consult co){
		if(co!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(co.id);
			sb.append(PREX+getValue(co.consultId));
			sb.append(PREX+getValue(co.content));
			sb.append(PREX+getValue(co.sendTime));
			return sb.toString();
		}else{
			return null;
		}
	}
	
	// int id,int appType,String receiverMobile,String isRead,String replyContent,
	// String replyTime
	public static ImportNoticeChild ParseImportNoticeChild(String strImportNoticeChild){
		try{
			String[] myString = strImportNoticeChild.split(PREX);	
			if (myString.length == 5){
				ImportNoticeChild myImportNoticeChild = new ImportNoticeChild(
						Integer.valueOf(myString[0]),
						myString[1],
						myString[2],
						myString[3],
						myString[4]);
				return myImportNoticeChild;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}

	// int id,int appType,String receiverMobile,String isRead,String replyContent,
	// String replyTime
	public static String formatImportNoticeChild(ImportNoticeChild inc){
		if(inc!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(inc.id);
			sb.append(PREX+getValue(inc.appType));
			sb.append(PREX+getValue(inc.receiverMobile));
			sb.append(PREX+getValue(inc.isRead));
			sb.append(PREX+getValue(inc.replyContent));
			sb.append(PREX+getValue(inc.replyTime));
			return sb.toString();
		}else{
			return null;
		}
	}
	
//	// public Match(String matchId, String isGroup, String beginTime,
//	String cnTitle, String day, String memo, String enTitle,
//	String isSeed, String matchTime, String sectionCnName,
//	String sectionEnName, String statics, String turnId,
//	String venueId, String playerA, String playerRetail,
//	String playerB, String winner, String groupA, String groupRetail,
//	String groupB, String groupWinner, String enMatchTime,
//	String endTime) 
	
//	"SELECT ISNULL(ID,'')"+
//	"&';;'&ISNULL(IS_GROUP,'')
//	&';;'&ISNULL(BEGIN_TIME,'')
//	&';;'&ISNULL(CN_TITLE,'')
//	&';;'&ISNULL(day,'')
//	&';;'&ISNULL(EN_TITLE,'')	
//	&';;'&ISNULL(IS_SEED,'')
//	&';;'&ISNULL(MATCH_TIME,'')
//	&';;'&ISNULL(SECTION_CN_NAME,'')
//	&';;'&ISNULL(SECTION_EN_NAME,'')
//	&';;'&ISNULL(STATICS,'')
//	&';;'&ISNULL(TURN_ID,'')
//	&';;'&ISNULL(VENUE_ID,'')
//	&';;'&ISNULL(PLAYER_A,'')
//	&';;'&ISNULL(PLAYER_RETAIL,'')
//	&';;'&ISNULL(PLAYER_B,'')
//	&';;'&ISNULL(WINNER,'')
//	&';;'&ISNULL(GROUP_A,'')
//	&';;'&ISNULL(GROUP_RETAIL,'')
//	&';;'&ISNULL(GROUP_B,'')
//	&';;'&ISNULL(GROUP_WINNER,'')
//	&';;'&ISNULL(NUM,' ')
//	FROM atp_match
//	WHERE day = DAY(curdate());"
	
	// 30	;;	NO	;;	10：00	;;	第一轮2	;;	2	;;		;;		;;		;;		;;		;;	0	;;	1	;;	2	;;	48	;;		;;	58	;;	48	;;		;;		;;		;;		;;	2

	
	public static Match ParseMatch(String strMatch){
		try{
			String[] myString = strMatch.split(PREX);	
			if (myString.length == 22){
				Match myMatch = new Match(
						myString[0],
						myString[1],
						myString[2],
						myString[3],
						myString[4],
						"",
						myString[5],
						myString[6],
						myString[7],
						myString[8],
						myString[9],
						myString[10],
						myString[11],
						myString[12],
						myString[13],
						myString[14],
						myString[15],
						myString[16],
						myString[17],
						myString[18],
						myString[19],
						myString[20],
						"",
						"",
						myString[21]);
				return myMatch;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
//	"SELECT &';;'&ISNULL(ID,'')
//	&';;'&ISNULL(CN_NAME,'')
//	&';;'&ISNULL(EN_NAME,'')
//	&';;'&ISNULL(MEMO,'')
//	&';;'&ISNULL(NUM,' ')
//	&';;'&ISNULL(GAME_ID,'');
//	FROM t_cms_venues;"
	
//	public Venue(String venueId, String cnName, String enName, String memo,
//			String num, String gameId) 
	
	public static Venue ParseVenue(String strVenue){
		try{
			String[] myString = strVenue.split(PREX);	
			if (myString.length == 6){
				Venue myVenue = new Venue(
						myString[0],
						myString[1],
						myString[2],
						myString[3],
						myString[4],
						myString[5]);
				return myVenue;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
//	"SELECT ISNULL(ID,'')
//	&';;'&ISNULL(NUM,'')
//	&';;'&ISNULL(SCORE_A,'')
//	&';;'&ISNULL(SCORE_B,'')
//	&';;'&ISNULL(SMALL_SCORE_A,'')
//	&';;'&ISNULL(SMALL_SCORE_B,'')
//	&';;'&ISNULL(MATCH_ID,' ');
//	FROM T0.atp_score LEFT JOIN T1.atp_match ON T0.MATCH_ID = T1.ID
//	WHRE T0.day = DAY(curdate());"
	
//	public Score(String scoreId, String num, String scoreA, String scoreB,
//			String smallScoreA, String smallScoreB, String matchId)
	
	
	public static Score ParseScore(String strScore){
		try{
			String[] myString = strScore.split(PREX);	
			if (myString.length == 7){
				Score myScore = new Score(
						myString[0],
						myString[1],
						myString[2],
						myString[3],
						myString[4],
						myString[5],
						myString[6]);
				return myScore;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	
	public static Game ParseGame(String strGame){
		try{
			String[] myString = strGame.split(PREX);	
			if (myString.length == 4){
				Game myGame = new Game(
						myString[0],
						myString[1],
						myString[2],
						myString[3]						
						);
				return myGame;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	
//	select ID,PLAYER_A,PLAYER_B,GROUP_SEED_NUMBER,GROUP_WILDCARD
//	FROM atp_player_group
	
	
	public static PlayerGroup ParsePlayerGroup(String strPlayerGroup){
		try{
			String[] myString = strPlayerGroup.split(PREX);	
			if (myString.length == 5){
				PlayerGroup myPlayerGroup = new PlayerGroup(
						myString[0],
						myString[1],
						myString[2],
						myString[3],
						myString[4]
						);
				return myPlayerGroup;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	
//	select ID,CN_NAME,CN_SNAME,COUNTRY,
//	EN_COUNTRY,EN_NAME,EN_SNAME,SEED_NUMBER,
//	BAT,BIRTHDAY,BIRTH_ADDRESS,EN_BIRTHADDRESS,
//	PERMANENT_ADDRESS,EN_PERMANENTADDRESS,
//	HEIGHT,WEIGHT,NUM,WILDCARD FROM atp_player
	
	public static Player ParsePlayer(String strPlayer){
		try{
			String[] myString = strPlayer.split(PREX);	
			if (myString.length == 18){
				Player myPlayer = new Player(
						myString[0],
						myString[1],
						myString[2],
						myString[3],
						myString[4],
						myString[5],
						myString[6],
						myString[7],
						myString[8],
						myString[9],
						myString[10],
						myString[11],
						myString[12],
						myString[13],
						myString[14],
						myString[15],
						myString[16],
						myString[17]
						);
				return myPlayer;
			}else{			
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	// 
	
	
	
	public static String getValue(Object v){
		if(v==null){
			return "";
		}else{
			return String.valueOf(v);
		}
	}
	
	public static void main(String[] args){
		 String xx = "55;;0;;{算法的放大 <br>};;{};;1;;2010-09-16 12:00;;1;;2010-09-16 12:00;;天气预报;;1";
		 ParseImportNotice(xx);
	}
}